﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Evpo.ContactData.ViewModel;
using Evpo.ContactData.Common;
using System.Collections.Specialized;
using Evpo.ContactData.UI.Tools;
using Evpo.ContactData.UI.GeneralClasses;

namespace Evpo.ContactData.UI
{
    public partial class AddressList : ItemList
    {
        #region Contants
        #endregion

        #region Properties
        protected override string ItemInListControlName
        {
            get { return "uiItemInList"; }
        }

        protected override ListViewModel BaseViewModel
        {
            get { return ViewModel; }
        }

        protected override ListView ListView
        {
            get { return uiListView; }
        }

        protected AddressListViewModel ViewModel
        {
            get;
            set;
        }

        protected UrlComposer UrlCmp
        {
            get;
            set;
        }

        #endregion

        #region Events
        private void OnAddressSelected(int addressID)
        {
            if (AddressSelected != null)
                AddressSelected(this, new SelectedItemEventArgs(addressID));
        }

        public event EventHandler<SelectedItemEventArgs> AddressSelected;
        #endregion Events


        #region Handlers

        protected void Page_Init(object sender, EventArgs e)
        {
            AddressQueryParameters parameters = 
                UrlComposer.ExtractQueryParameters(Request.QueryString);

            ViewModel = new AddressListViewModel(
                new ModelFactory(),
                (IConnection)Session[SessionStateKeys.Connection],
                parameters,
                Preferences.PageCount);

            UrlCmp = new UrlComposer(ViewModel, Request.Url);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                DataBind();
        }

        protected void uiSearch_Click(object sender, EventArgs e)
        {
            ViewModel.AddressQuery.LinesContain = uiLinesContain.Text;
            ViewModel.AddressQuery.SuburbContains = uiSuburbContains.Text;
            ViewModel.AddressQuery.State = uiState.Text;
            ViewModel.AddressQuery.Postcode = uiPostcode.Text;
            ViewModel.AddressQuery.Country = uiCountry.Text;
            ViewModel.GoToFirstPage();
            Server.Transfer(UrlCmp.CurrentUrl.PathAndQuery);
        }

        protected void uiSelect_Click(object sender, EventArgs e)
        {
            WebControl uiButton = (WebControl)sender;
            AddressInList uiAddressInList = (AddressInList)uiButton.Parent.FindControl("uiItemInList");
            OnAddressSelected(uiAddressInList.ItemID);
        }

        #endregion

        #region Fields
        #endregion Fields

        #region Methods

        #endregion
        
        #region Classes
        protected class QueryKeys
        {
            internal const string PageBegin = "PageBegin";
            internal const string PageEnd = "PageEnd";
            internal const string LinesContain = "LinesContain";
            internal const string SuburbContains = "SuburbContains";
            internal const string State = "State";
            internal const string Postcode = "Postcode";
            internal const string Country = "Country";
        }
        #endregion


    }
}